package cn.tedu._04weibo.base.interceptors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import java.time.LocalDateTime;


@Slf4j
public class TimeAccessInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("进入到限定访问时间的拦截器");
        LocalDateTime now=LocalDateTime.now();
        if(now.getHour()<10||now.getHour()>20){
            //第一种重定向页面
//            response.sendRedirect("/error.html");
//            return false;
            //第二种返回错误信息
            throw new RuntimeException("访问时间必须是10:00-20:00之间");
        }
        return true;
    }
}
